import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;

import javax.swing.JComponent;
import javax.swing.event.MouseInputListener;


public class pong extends JComponent implements MouseListener, MouseInputListener, Runnable{

	private int mouseX, mouseY;
	public int pOneDir;
	public int pTwoDir;
	private int [][] playerOne = new int[2][10];
	private int [][] playerTwo = new int[2][10];
	KeyController controller = new KeyController();
	int p1ly, p1lx;
	int p2ly, p2lx;
	boolean p1fire, p2fire;
	int projx = 230;
	int projy = 130;
	int p1charge = 4;
	int p2charge = 4;
	//vectors for the slope of the bullet
	int projSlopeX = 0;
	int projSlopeY = 0;
	//int that determines start of next ball, towards whoever was scored on last
	int lastScore;
	int p1score = 0;
	int p2score = 0;
	Random rand = new Random();
	double xcount = 3;
	double lasttime = System.currentTimeMillis();
	double thistime = System.currentTimeMillis();
	public pong(){

		Thread t = new Thread(this);
		setup();
		addMouseListener(this);
		addKeyListener(controller);
		addMouseMotionListener(this);
		t.start();
	}

	private void setup() {
		for(int i = 0; i < 2; i++)
			for(int j = 0; j < 10; j++)
				playerOne[i][j] = 1;

		for(int i = 0; i < 2; i++)
			for(int j = 0; j < 10; j++)
				playerTwo[i][j] = 1;



	}

	public static void main(String[] args) {
		new frame();
	}

	@Override
	public void run() {
		mouseX = 1;
		mouseY = 1;
		projectile(0);

		while(true){
			//projSlopeX = xcount;
			/*for(int i = 0; i < 255; i++)
				if(KeyController.keys[i] != 0){
					switch(KeyController.keys[i]){
					case 40:

					case 38:

					}
				}else{

				}
			 */

			try {
				Thread.sleep(30);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}



			//top bounce
			if(projy < 5){
				projSlopeY = -projSlopeY;
			}
			//bottom bounce
			if(projy > 270){
				projSlopeY = -projSlopeY;
			}


			//left player score
			if(projx > 480){
				lastScore = 1;
				projectile(0);
				p1score++;
				xcount += 0.5;

				//right player score
			}else if (projx<-5){
				lastScore = 0;
				projectile(0);
				p2score++;
				xcount+=0.5;

			}


			projectile(1);
			pOneDir = controller.getPOneDir();
			pTwoDir = controller.getPTwoDir();
			repaint();
		}
	}

	public void paintComponent(Graphics g){
		//10x10
		g.setColor(Color.BLUE);
		for(int i = 0; i < 2; i++){
			for(int j = 0; j < 10; j++){
				if(playerOne[i][j] == 1){
					g.fillRect((i*10)+15, (j*10)+(320/2)-(5*15)+(pOneDir*10), 10, 10);
					//player lasers!!
					if(controller.pOneFire){
						System.out.println("Tyring to fire, p1");
						if(p1charge>3 ){
							System.out.println("p1 fire");
							p1lx = (i*10)+15;
							p1ly = (j*10)+(320/2)-(5*15)+(pOneDir*10);
							p1fire = true;
						}
						controller.pOneFire = false;
					}
					//left laser hit
					if(p2lx >= (i*10)+15 && p2lx <= (i*10)+25 && p2ly >= (j*10)+(320/2)-(5*15)+(pOneDir*10)-10 && p2ly <= (j*10)+(320/2)-(5*15)+(pOneDir*10) + 12){
						playerOne[i][j] = 0;
						p2fire = false;
					}
					//left bounce
					if(projx >= (i*10)+15 && projx <= (i*10)+25 && projy >= (j*10)+(320/2)-(5*15)+(pOneDir*10)-10 && projy <= (j*10)+(320/2)-(5*15)+(pOneDir*10) + 12){
						
						thistime = System.currentTimeMillis();

						if(thistime - lasttime > 200){
							projSlopeX = -projSlopeX;	
							playerOne[i][j] = 0;
							p1charge++;

							System.out.println("Player One's Score: " + p1score + " Player One's charge: " + p1charge);

							lasttime = thistime;
						}
					}
				}else{
					//System.out.println("DNE");
				}
			}
		}
		g.setColor(Color.RED);
		for(int i = 0; i < 2; i++){
			for(int j = 0; j < 10; j++){
				if(playerTwo[i][j] == 1){
					g.fillRect((i*10)+425, (j*10)+(320/2)-(5*15)+(pTwoDir*10), 10, 10);
					
					if(controller.pTwoFire){
						
						System.out.println("Tyring to fire, p2");
						if(p2charge>3){
							System.out.println("p2 fire");
							p2ly = (j*10)+(320/2)-(5*15)+(pTwoDir*10);
							p2lx = (i*10)+425;
							p2fire = true;
						}
						controller.pTwoFire = false;
					}
					//right laster hit
					if(p1lx >= (i*10)+15 && p1lx <= (i*10)+25 && p1ly >= (j*10)+(320/2)-(5*15)+(pTwoDir*10)-10 && p1ly <= (j*10)+(320/2)-(5*15)+(pTwoDir*10) + 12){
						playerTwo[i][j] = 0;
						p1fire = false;
					}
					//right bounce
					if(projx >= (i*10)+425 && projx <= (i*10)+435 && projy >= (j*10)+(320/2)-(5*15)+(pTwoDir*10)-10 && projy <= (j*10)+(320/2)-(5*15)+(pTwoDir*10) + 12){
						thistime = System.currentTimeMillis();
						
						if(thistime - lasttime > 200){
							projSlopeX = -projSlopeX;	
							playerTwo[i][j] = 0;
							p2charge++;

							System.out.println("Player Two's Score: " + p2score+ " Player Two's charge: " + p2charge);

							lasttime = thistime;
						}
					}
				}else{
					//System.out.println("DNE");
				}
			}
		}


		g.setColor(Color.GREEN);
		g.fillRect(projx, projy, 10, 10);
		
		if(p1fire){
			g.setColor(Color.cyan);
			g.fillOval(p1lx, p1ly , 11, 11);
			//g.fillRect(p1lx-2, p1ly, 3,15);
			p1lx = p1lx + 5;
		}
		if(p2fire){
			g.setColor(Color.orange);
			g.fillOval(p2lx, p2ly , 11, 11);
			//g.fillRect(p2lx+2, p2ly, 3,15);
			p2lx = p2lx - 5;
		}
	}


	public void projectile(int cond){
		//0 for resetting, 1 for  the motion, 2 for bounce, 3 for scoring

		if(cond == 0){
			System.out.println("cond 0!");
			projx = 230;
			projy = 130;
			if(lastScore == 1){
				projSlopeX = (int)xcount;
				projSlopeY = rand.nextInt() % 5;
				if(projSlopeY  <= 1){
					projSlopeY = 3;
				}
			}else{
				projSlopeX = (int)-xcount;
				projSlopeY = rand.nextInt() % 5;
				if(projSlopeY  <= 1){
					projSlopeY = 3;
				}
			}
			//projectile(1);
		}
		if(cond == 1){
			//System.out.println("cond 1!");
			projx += projSlopeX;
			projy += projSlopeY;
		}
	}

	@Override
	public void mouseDragged(MouseEvent m) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseMoved(MouseEvent m) {
		//System.out.println("MOUSE");
		mouseY = m.getY();
		mouseX = m.getX();
	}

	public void keyPressed(KeyEvent k) {



	}


	public void keyReleased(KeyEvent k) {
		// TODO Auto-generated method stub

	}


	public void keyTyped(KeyEvent k) {
		// TODO Auto-generated method stub

	}


	public void mouseClicked(MouseEvent m) {
		System.out.println(m.getX());
		System.out.println(m.getY());
		projectile(0);
		System.out.println("resetting");

	}

	@Override
	public void mouseEntered(MouseEvent m) {

	}

	@Override
	public void mouseExited(MouseEvent m) {

	}

	@Override
	public void mousePressed(MouseEvent m) {

	}

	@Override
	public void mouseReleased(MouseEvent m) {

	}

}
